package com.lyb.ui.authentication.db;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.lyb.ui.authentication.bean.ContactBean;

import java.util.ArrayList;
import java.util.List;

public class DatabaseManager {
    private Context        mCtx;
    private DatabaseHelper instance;
    private SQLiteDatabase writableDatabase;

    private List<ContactBean> mContractList = new ArrayList<>();
    private List<String>      mPhoneList    = new ArrayList<>();

    public DatabaseManager(Context ctx) {
        this.mCtx = ctx;
    }

    public DatabaseManager open() throws SQLException {
        instance = DatabaseHelper.getInstance(mCtx);
        writableDatabase = instance.getWritableDatabase();
        return this;
    }

    /**
     * 添加联系人信息 insert into ContactTab  values (0,'17629000032','xiccc',1,'1004')
     */
    public void addContact(int id, String phone, String name, int status, String userid) {
        String insertSql = "insert into " + DatabaseHelper.TAB_CONTACT + " values (" + id + ",'" + phone + "','" +
                name + "'," + status + ",'" + userid + "')";
        writableDatabase.execSQL(insertSql);
    }

    /**
     * 是否存在该手机号  select * from ContactTab where MOBILENUM = '17600000000'
     */
    public boolean isExists(String phone) {

        String selectSql = "select * from " + DatabaseHelper.TAB_CONTACT + " where MOBILENUM = '" + phone + "'";

        Cursor cursor = null;

        try {
            cursor = writableDatabase.rawQuery(selectSql, null);
            if (cursor != null) {
                int count = cursor.getCount();
                if (count != 0) {
                    return true;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
        }

        return false;
    }

    /**
     * 获取所有联系人
     */
    public List<ContactBean> getContactList() {
        if (mContractList != null) {
            mContractList.clear();
        }
        String selectSql = "select * from " + DatabaseHelper.TAB_CONTACT;
        Cursor cursor = null;
        try {
            cursor = writableDatabase.rawQuery(selectSql, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    ContactBean contactBean = new ContactBean();
                    contactBean.setId(cursor.getInt(0));
                    contactBean.setMobile(cursor.getString(1));
                    contactBean.setName(cursor.getString(2));
                    contactBean.setStatus(cursor.getInt(3));
                    contactBean.setUser_id(cursor.getString(4));
                    mContractList.add(contactBean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
        }
        return mContractList;
    }


    public List<String> getPhoneList() {
        if (mPhoneList != null) {
            mPhoneList.clear();
        }
        String selectSql = "select * from " + DatabaseHelper.TAB_CONTACT;
        Cursor cursor = null;
        try {
            cursor = writableDatabase.rawQuery(selectSql, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    mPhoneList.add(cursor.getString(1));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
        }
        return mPhoneList;
    }


    public void beginTransaction() {
        writableDatabase.beginTransaction();
    }


    public void endTransaction(boolean isSuccess) {
        if (isSuccess)
            writableDatabase.setTransactionSuccessful();
        writableDatabase.endTransaction();
    }

    public void close() {
        if (writableDatabase != null) {
            writableDatabase.close();
        }
        if (instance != null) {
            instance.close();
        }
    }
}
